//
//  editMyResumeTextResumeVC.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/4/18.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit

class editMyResumeTextResumeVC: UITableViewController {
    
    var headView:UIView?
    
    var myResumeDict = [String:AnyObject]()
    var myResumeModel:LDMyResumeModel?
    var workExpArr = [LDMyResumeWorkexperienceModel]()
    var eduExpArr = [LDMyResumeEduexperienceModel]()
    
    var addTapCellTitleArr = ["","","＋ 添加工作经历","＋ 添加教育经历"]
    // 亮点标签
    var highlightsParams = [String]()
    var cellDict = [Dictionary<String,AnyObject>]()
    //
    var titleArr = [Int:String]()
    var descArr = [Int:String]()
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        view.frame = LDFrame.ScreenBounds
        tableView.contentInset = UIEdgeInsetsMake(45, 0, 0, 0)
        tableView.backgroundColor = LDGolbalColor.BgGoundGray
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        
        headView = UIView.init()
        headView?.backgroundColor = UIColor.colorWithHexString("#fffb93", alapha: 1)
        headView?.frame = CGRectMake(0, 0, LDFrame.ScreenWidth, 40)
        headView?.userInteractionEnabled = true
        
        let lab = UILabel.setDetailsLab("在电脑上编辑微简历 >", fontSize: 15, titleColor: UIColor.colorWithHexString("#daa34e", alapha: 1), Alignment: .Center)
        headView?.addSubview(lab)
        lab.userInteractionEnabled = true
        lab.anchorInCenter(width: LDFrame.ScreenWidth, height: 20)
        
        headView?.bk_whenTapped({ [weak self] in
//            self?.editInfoFromDeskTop()
            self?.showLDAlertView("请在电脑上打开网址\nhttp://114.55.59.142/web/")
            
            })
        
        tableView.tableHeaderView = headView
        setDefaultData()
        
    }
    
    
    override func viewDidAppear(animated: Bool) {
        
        LDUserInfo.sharedInstance.updataJobFinderMyResume({ [weak self] (dict) in
            
            
            self?.setDefaultData()
            self?.tableView.reloadData()
            
            }, failure: nil)
        
    }
    
    //得到tag的id数组
    func getMyHeightIDArrFromDictArr() ->Array<String>{
        
        var titleArr = [String]()
        
        for item in (myResumeModel?.highlights)! {
            
            let highlights = LDMyResumeHighlightModel.yy_modelWithDictionary(item as! [NSObject : AnyObject])
            
            titleArr.append(highlights!.tagid)
        }
        
        return titleArr
    }
    
    
    //设置基本数据
    func setDefaultData() {
        
        myResumeDict = LDUserInfo.sharedInstance.myRemuseInfoDict
        let dict = LDUserInfo.sharedInstance.myRemuseInfoDict
        
        myResumeModel = LDMyResumeModel.yy_modelWithDictionary(dict)
        highlightsParams = getMyHeightIDArrFromDictArr()
        
        if myResumeModel?.workexperience == nil || myResumeModel?.eduexperience == nil {
            
            return
        }
        
        
        var workarr = [LDMyResumeWorkexperienceModel]()
        for item in myResumeModel!.workexperience {
            
            let model = LDMyResumeWorkexperienceModel.yy_modelWithDictionary(item as! [NSObject : AnyObject])
            workarr.append(model!)
        }
        workExpArr = workarr
        
        
        var eduarr = [LDMyResumeEduexperienceModel]()
        for item in myResumeModel!.eduexperience {
            
            let model = LDMyResumeEduexperienceModel.yy_modelWithDictionary(item as! [NSObject : AnyObject])
            eduarr.append(model!)
        }
        eduExpArr = eduarr
    }
    
    func editInfoFromDeskTop() {
        
        var style = LBXScanViewStyle()
        style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle.Inner
        style.photoframeLineW = 2
        style.photoframeAngleW = 18
        style.photoframeAngleH = 18
        style.isNeedShowRetangle = false
        style.anmiationStyle = LBXScanViewAnimationStyle.LineMove
        
        style.colorAngle = LDGolbalColor.GreenAssist
        style.animationImage = UIImage(named: "CodeScan.bundle/qrcode_Scan_weixin_Line")
        
        let vc = LDBaseQRScanVC()
        vc.title = "扫描二维码"
        vc.scanStyle = style
        
        navigationController?.pushViewController(vc, animated: true)
    }
}
extension editMyResumeTextResumeVC {
    // MARK: - Table view data source
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 5
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 2  {
            
            return workExpArr.count + 1
        } else if section == 3  {
            
            return eduExpArr.count + 1
        } else {
            
            return 1
        }
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        switch indexPath.section {
            
        case 0:
            let cell = creatCell(indexPath) as! baseMySettingCells
            
            let num = myResumeModel?.highlightscount ?? 0
            cell.descLab?.text = "\(num)个亮点"
            cell.titleLab?.text = "我的亮点"
            return cell
            
        case 1:
            let cell = creatCell(indexPath) as! baseMySettingCells
            cell.titleLab?.text = "我的优势"
            cell.descLab?.text = ""
            return cell
            
        case 2:
            
            let rowNum = workExpArr.count
            
            if indexPath.row != rowNum {
                let cell = creatCell(indexPath)
                return cell
                
            } else {
                
                let cell = creatAddBtnCell(indexPath, title: addTapCellTitleArr[indexPath.section],tableView: tableView)
                return cell
            }
        case 3:
            
            let rowNum = eduExpArr.count
            
            if indexPath.row != rowNum {
                
                let cell = creatCell(indexPath)
                return cell
                
            } else {
                let cell = creatAddBtnCell(indexPath, title: addTapCellTitleArr[indexPath.section],tableView: tableView)
                return cell
            }
            
        case 4:
            
            var cell = tableView.dequeueReusableCellWithIdentifier("cellswich") as? editMyResumeVoiceResume_hiddenSwitch
            if cell == nil {
                
                cell = editMyResumeVoiceResume_hiddenSwitch.init(style: .Default, reuseIdentifier: "cellswich")
            }
            return cell!
            
        default:
            return UITableViewCell()
        }
        
    }
    
    func creatCell(indexPath:NSIndexPath) -> UITableViewCell {
        
        let cell = creatDefaultCell(indexPath)
        return cell
    }
}

extension editMyResumeTextResumeVC {
    
    //
    func creatDefaultCell(indexPath:NSIndexPath) ->UITableViewCell{
        
        var cell = tableView.dequeueReusableCellWithIdentifier("cells") as? baseMySettingCells
        if cell == nil {
            cell = baseMySettingCells.init(style: .Default, reuseIdentifier: "cells")
        }
        
        if indexPath.section == 2 {
         
            cell!.titleLab?.text =  workExpArr[indexPath.row].companyname//["companyname"] as? String ?? "NULL"
            let startStr = workExpArr[indexPath.row].startdate //as? String ?? "NULL"
            let endStr = workExpArr[indexPath.row].enddate//["enddate"] as? String ?? "NULL"
            cell?.descLab?.text = String(startStr) + "-" + String(endStr)
            
        } else if indexPath.section == 3 {
  
            cell?.titleLab?.text = eduExpArr[indexPath.row].institutename//["institutename"] as? String ?? "NULL"
            let startStr = eduExpArr[indexPath.row].startdate//["startdate"] as? String ?? "NULL"
            let endStr = eduExpArr[indexPath.row].enddate//["enddate"] as? String ?? "NULL"
            cell?.descLab?.text = String(startStr) + "-" + String(endStr)
            
        }
        return cell!
    }

    //添加按钮
    func creatAddBtnCell(indexPath:NSIndexPath,title:String,tableView:UITableView) ->UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("addcells")
        if cell == nil {
            
            cell = UITableViewCell.init(style: .Default, reuseIdentifier: "addcells")
            let lab = UILabel.setDetailsLab(title, fontSize: 15, titleColor: LDFontColor.Green, Alignment: .Center)
            lab.userInteractionEnabled = true
            cell?.contentView.addSubview(lab)
            
            lab.snp_makeConstraints(closure: { (make) in
                make.top.left.equalTo(0)
                make.bottom.right.equalTo(0)
            })
            //tap添加按钮
            lab.bk_whenTapped({ [weak self] in
                
                switch indexPath.section {
                case 2:
                    let vc = editMyResumeAddEditJobExperience()
                    self?.navigationController?.pushViewController(vc, animated: true)
                case 3:
                    let vc = editMyResumeAddOREditEdu()
                    self?.navigationController?.pushViewController(vc, animated: true)
                case 4:
                    let vc = editMyResumeAddOREditEdu()
                    self?.navigationController?.pushViewController(vc, animated: true)
                default:
                    break
                }
                })
        }
        return cell!
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        switch indexPath.section {
        case 4:
            return 60
        default:
            return 50
        }
    }
    
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headview = UIView()
        headview.backgroundColor = LDGolbalColor.BgGoundGray
    
        return headview
    }
    
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        switch section {
        case 0:
            return 0
        case 4:
            return 5
        default:
            return 10
        }
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print(indexPath.section,indexPath.row)
        if indexPath.section == 0 && indexPath.row == 0 {
            
            let vc = editMyResumeHightLight()
            vc.title = "我的亮点"
            vc.preSelHightIDParams = highlightsParams
            navigationController?.pushViewController(vc, animated: true)
            
        } else if indexPath.section == 1 {
            
            let vc = editMyResumeAdvantage()
            navigationController?.pushViewController(vc, animated: true)
            
        } else if indexPath.section == 2 {
            
            let vc = editMyResumeAddEditJobExperience()
            vc.type = .Edit
            vc.workModel = workExpArr[indexPath.row]
            
            navigationController?.pushViewController(vc, animated: true)
            
        } else if indexPath.section == 3 {
            
            let vc = editMyResumeAddOREditEdu()
            vc.type = .Edit
            vc.eduModel = eduExpArr[indexPath.row]
            navigationController?.pushViewController(vc, animated: true)
            
        } else if indexPath.section == 4 {
            
        }
    }
}